﻿package com.wly.interval
{
	import flash.events.Event;

	public class IntervalManager
	{
		private var dynamicObjArr:Object;
		static private var IDKEY:int;
		
		static private var _instance:IntervalManager;
		static public var TICKCOUNT:int;
		
		public function IntervalManager()
		{
			dynamicObjArr = new Object;
			IDKEY = 0;
			_instance = this;
		}
		
		static public function getInstance():IntervalManager
		{
			if(!_instance)
			{
				_instance = new IntervalManager();
			}
			return _instance;
		}
		
		public function registerObj(obj:IntervalCallBack):int
		{
			dynamicObjArr[IDKEY] = obj;
			obj.id = IDKEY;
			return IDKEY++;
		}
		
		public function unRegisterObjById(id:int):void
		{
			var obj:IntervalCallBack;
			for each(obj in dynamicObjArr)
			{
				if(obj && obj.id==id)
				{
					unRegisterObj(obj);
					return;
				}
			}
		}
		
		public function unRegisterObj(obj:IntervalCallBack):void
		{
			if(!obj)
			{
				return;
			}
			
			dynamicObjArr[obj.id] = null;
			delete dynamicObjArr[obj.id];
		}
		
		public function onFrame(e:Event):void
		{
			TICKCOUNT++;
			var obj:IntervalCallBack;
			 for each(obj in dynamicObjArr)
			 {
				 if(obj && obj.callbackFunc!=null)
				 {
				 	obj.callbackFunc(obj.param);
				 }
			 }
		}
	}
}